package com.cardvalue.sys.activity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.Button;
import android.widget.EditText;
import com.cardvalue.sys.R;
import com.cardvalue.sys.base.BaseActivity;
import com.cardvalue.sys.cache.LocalCache;
import com.cardvalue.sys.mode.LoginService;
import com.cardvalue.sys.tool.CheckingForm;
import com.cardvalue.sys.tool.MD5Util;
import com.cardvalue.sys.tool.MTextWatcher;
import com.cardvalue.sys.tool.MessageBox;
import com.cardvalue.sys.tool.Utiltools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 修改密码
 */
public class ChangePwdActivity extends BaseActivity {
    @Bind(R.id.curPwd) EditText   oldPwd;//旧密码
    @Bind(R.id.newPwd) EditText   pwd1;//新密码
    @Bind(R.id.newPwd1) EditText   pwd2;//确认密码
    @Bind(R.id.button1) Button mSubmit;
    private LoginService  updatePwdService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.bind(this);
        setHeaderFields(0, R.string.changepwd,0,R.mipmap.back, 0,0);
        updatePwdService=new LoginService(this);


        oldPwd.addTextChangedListener(new MTextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if(s.length()>0&&pwd1.length()>0&&pwd2.length()>0){
                    mSubmit.setBackgroundResource(R.drawable.accept_button_style);
                }else{
                    mSubmit.setBackgroundResource(R.drawable.disabled_button_style_cc);
                }
            }
        });


        pwd1.addTextChangedListener(new MTextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if(s.length()>0&&oldPwd.length()>0&&pwd2.length()>0){
                    mSubmit.setBackgroundResource(R.drawable.accept_button_style);
                }else{
                    mSubmit.setBackgroundResource(R.drawable.disabled_button_style_cc);
                }
            }
        });

        pwd2.addTextChangedListener(new MTextWatcher(){
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                if(s.length()>0&&pwd1.length()>0&&oldPwd.length()>0){
                    mSubmit.setBackgroundResource(R.drawable.accept_button_style);
                }else{
                    mSubmit.setBackgroundResource(R.drawable.disabled_button_style_cc);
                }
            }
        });


    }


    @OnClick(R.id.button1) void clickSubmit(){
        Utiltools.printE("修改密码","点击了");
        if(!CheckingForm.checkForChangepwdForm(oldPwd.getText().toString()
                ,pwd1.getText().toString(),pwd2.getText().toString())){
            MessageBox.ToastShow(CheckingForm.LastError, ChangePwdActivity.this);
            return ;
        }else{
            updatePwdService.updatePwdService(MD5Util.MD5(oldPwd.getText().toString()),
                    MD5Util.MD5(pwd1.getText().toString()));
            Utiltools.printE("修改密码",oldPwd.getText().toString()+"-"+pwd1.getText().toString()
                    +"==="+ LocalCache.getStringLocalData(this,"objectId"));
        }

    }

}
